﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _38_CheckboxList.Models;
using System.Text;

namespace _38_CheckboxList.Controllers
{
    public class HomeController : Controller
    {
        private SampleDBContext db = new SampleDBContext();

        //
        // GET: /Home/
         [HttpGet]
        public ActionResult Index()
        {
            return View(db.Cities);
        }

        [HttpPost]
        public string Index(IEnumerable<City> cities)
        {
            if (cities.Count(x => x.isSelected) == 0)
            {
                return "You have not selected any City";
            }

            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("You have selected =");
                foreach (var item in cities)
                {
                    if (item.isSelected)
                    {
                        sb.Append(item.Name + ",");
                    }

                }
                sb.Remove(sb.ToString().LastIndexOf(","), 1);
                return sb.ToString();
            }
        }

        //
        // GET: /Home/Details/5

        public ActionResult Details(int id = 0)
        {
            City city = db.Cities.Single(c => c.ID == id);
            if (city == null)
            {
                return HttpNotFound();
            }
            return View(city);
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Home/Create

        [HttpPost]
        public ActionResult Create(City city)
        {
            if (ModelState.IsValid)
            {
                db.Cities.AddObject(city);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(city);
        }

        //
        // GET: /Home/Edit/5

        public ActionResult Edit(int id = 0)
        {
            City city = db.Cities.Single(c => c.ID == id);
            if (city == null)
            {
                return HttpNotFound();
            }
            return View(city);
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit(City city)
        {
            if (ModelState.IsValid)
            {
                db.Cities.Attach(city);
                db.ObjectStateManager.ChangeObjectState(city, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(city);
        }

        //
        // GET: /Home/Delete/5

        public ActionResult Delete(int id = 0)
        {
            City city = db.Cities.Single(c => c.ID == id);
            if (city == null)
            {
                return HttpNotFound();
            }
            return View(city);
        }

        //
        // POST: /Home/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            City city = db.Cities.Single(c => c.ID == id);
            db.Cities.DeleteObject(city);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}